﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ASPNETVN.PORTAL.Components.Share
{
    public partial class Comment
    {
        // Select by foreign key return collection		
        public static IDataReader SelectReaderBy_ModuleID_ItemID(Guid moduleID, int itemID)
        {
            const string spName = "p_SHARE_Comment_SelectBy_ModuleID_ItemID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, itemID);

            return db.ExecuteReader(dbCommand);
        }

        // Select by foreign key return collection		
        public static IList<Comment> SelectCollectionBy_ModuleID_ItemID(Guid moduleID, int itemID)
        {
            List<Comment> collection = new List<Comment>();
            SqlDataReader reader = (SqlDataReader)SelectReaderBy_ModuleID_ItemID(moduleID, itemID);
            while (reader.Read())
            {
                Comment entity = new Comment();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
                if (!reader.IsDBNull(reader.GetOrdinal("ItemID"))) entity.ItemID = reader.GetInt32(reader.GetOrdinal("ItemID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Body"))) entity.Body = reader.GetString(reader.GetOrdinal("Body"));
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetBoolean(reader.GetOrdinal("Status"));
                if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                collection.Add(entity);
            }
            reader.Close();
            return collection;
        }

        public static int DeleteBy_ModuleID_ItemID(Guid moduleID, int itemID)
        {
            string spName = "[dbo].[p_SHARE_Comment_DeleteBy_ModuleID_ItemID]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@ItemID", SqlDbType.Int, itemID);

            return db.ExecuteNonQuery(dbCommand);
        }
    }
}
